﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;

namespace EnhancedCQWP.EnhancedContentQuery
{
    [ToolboxItemAttribute(false)]
    public class EnhancedContentQuery : WebPart, IWebEditable
    {
        public EnhancedContentQuery()
        {
            ExportMode = WebPartExportMode.All;
        }

        private const string defaultSites = "";
        private string sites = defaultSites;
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string Sites
        {
            get { return sites; }
            set { sites = value; }
        }

        private const string defaultContentType = "Article Page";
        private string contentTypes = defaultContentType;
        [Category("Settings")]
        [DefaultValue(defaultContentType)]
        [Description("Content Type.")]
        [WebBrowsable]
        [Personalizable]
        public string ContentTypes
        {
            get { return contentTypes; }
            set { contentTypes = value; }
        }

        private const string defaultContentTypeProperties = "";
        private string _contentTypeProperties = defaultContentTypeProperties;
        [Category("Settings")]
        [DefaultValue(defaultContentTypeProperties)]
        [Description("ContentTypes Properties Field.")]
        [WebBrowsable]
        [Personalizable]
        public string ContentTypeProperties
        {
            get { return _contentTypeProperties; }
            set { _contentTypeProperties = value; }
        }

        private const string defaultXsltLink = "/Style Library/EnhancedCQWP/Xslt/SimpleDateAndTitle.xslt";
        private string xsltLink = defaultXsltLink;
        [Category("Settings")]
        [DefaultValue(defaultXsltLink)]
        [Description("The CSS style to use with the current node.")]
        [WebBrowsable]
        [Personalizable]
        public string XsltLink
        {
            get { return xsltLink; }
            set { xsltLink = value; }
        }

		private const string defaultExtraSites = "";
		private string extraSites = defaultExtraSites;
		[Category("Settings")]
		[DefaultValue(defaultExtraSites)]
		[Description("ExtraSites Properties Field.")]
		[WebBrowsable]
		[Personalizable]
		[WebDescription("The publishing sites from which we will display the pages. Semicolon seperated")]
        [HtmlDesigner("/_layouts/zoombldr.aspx", DialogFeatures = "dialogHeight:500px;dialogWidth:650px;help:no;status:no;resizable:yes")]
		public string ExtraSites
		{
			get { return extraSites; }
			set { extraSites = value; }
		}

        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/EnhancedCQWP/EnhancedContentQuery/EnhancedContentQueryUserControl.ascx";

        protected override void CreateChildControls()
        {
            var control = (EnhancedContentQueryUserControl)Page.LoadControl(_ascxPath);
            control.ContentTypes = ContentTypes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            control.ContentTypeProperties = ContentTypeProperties.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
			control.ExtraSites = extraSites.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries);
            control.XsltLink = xsltLink;
            Controls.Add(control);
        }

        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            var editors = new List<EditorPart>();
            editors.Add(new SiteSelectionEditorPart(this.ID));
            return new EditorPartCollection(editors);
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }
    }
}
